$(function(){
	var $searchVillageResult=$('.search-village-result');
	var $searchInput=$('#search-input');
	var $searchVillageFail=$('.search-village-fail');
	var historySearchRecordArr=[];
	historySearchRecordArr= common.getLocalData("historySearchRecord");
	if(historySearchRecordArr===null){
		common.setLocalStorage('historySearchRecord',JSON.stringify([]));
		historySearchRecordArr=[];
	}
	//成交房源的查询
	var hangSearchVal=common.getQueryString('hangSearchVal');
	// console.log(hangSearchVal);
	if(hangSearchVal!=null){
		//对数组进行去重；
		for(var i=0;i<historySearchRecordArr.length;i++){
			if(historySearchRecordArr[i]===hangSearchVal){
				historySearchRecordArr.splice(i,1);
			}
		}
		historySearchRecordArr.unshift(hangSearchVal);
		historyControl.buildCacheHistory("historySearchRecord",hangSearchVal);
	}
	// console.log(historySearchRecordArr);
	var $recordList = $('#record-list');
	var $searchBtn = $('#search-btn');
	var $resultNum = $('#village-num');
	var  $houseSaleListPanel=$('#searchVillage-result-list');
	var $resMsg=$("#res-msg");
	var globalHost=common.globalHost;
	var dealQueryVillageByName=common.dealQueryVillageByName;
	var url=globalHost+dealQueryVillageByName;
	var documentHeight=0;
    var windowHeight=0;
	var prevSearchVal=historySearchRecordArr[0];
	$searchInput.val(prevSearchVal);
	var sendData={};
		sendData={
	        pageIndex:1,
	        pageSize:10,
	        villageId:prevSearchVal
	    };
		historyControl.layerShow();
		requestData();
	//挂牌小区搜索
	$searchBtn.off('tap').on('tap',function(e){
		$houseSaleListPanel.html("");
		var $this=$(this);
		var checkCase=$this.hasClass("act-ago");
		var searchVal=$searchInput.val();
		sendData={
	        pageIndex:1,
	        pageSize:10,
	        villageId:searchVal
	    };
		if(!!checkCase&&searchVal.length>=2){
			$this.removeClass("act-ago");
			historyControl.layerShow();
	   		//初始化dom
			$searchVillageResult.hide();
	        $searchVillageFail.hide();
			requestData();
			$this.addClass("act-ago");
			//添加缓存；
			historyControl.buildCacheHistory("historySearchRecord",searchVal);
    	}
    	e.preventDefault();
        return false;
	});
	$(window).on("scroll",function (e) {
        windowHeight=$(window).height();
        if($(this).scrollTop()+windowHeight==documentHeight){
        	$resMsg.html('<span class="loading"></span>正在加载中呦~~').show();
            requestData();
        }
    });
	function requestData() {
        $.ajax({
            data:sendData,
            type: "POST",
            contentType:"application/x-www-form-urlencoded;charset=utf-8",
            dataType:"json",
            url:url,
            success:function(data){
            	 historyControl.layerHide();
                $resMsg.text("").hide();
                if(typeof data==="string"){
                    data=JSON.parse(data);
                }
                if(data.totalNumber==0){
                	$searchVillageResult.hide();
                	$searchVillageFail.show();
                }else{
                	$searchVillageResult.show();
                	$searchVillageFail.hide();
                	 $resultNum.html(data.totalNumber)
	                data=data.data;
	                if(!data.length){
	                    $resMsg.text("已经没有更多信息了").show();
	                }else{
	                    var responseDom=historyControl.buildVillageSearchResult({
	                        loadingSelector:$resMsg,
	                        imgBasePath:globalHost,
	                        jumpLink:"houseList.html",
	                        pageTargetType:"_self",
	                        dataList:data,
	                        searchWord:sendData.villageId
	                    });
	                    $houseSaleListPanel.append(responseDom);
	                    documentHeight=$(document).height();
	                    sendData.pageIndex++;
	                }
                }
            },
            timeout:10000,
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                historyControl.layerHide();
                // console.log("网络不畅，请稍后重试");
                historyControl.errDialog("网络不畅，请稍后重试");
                
            }
        });
    }
})
